package cn.hnh.service.impl;

import cn.hnh.constrant.ResultInfo;
import cn.hnh.mapper.ArticleMapper;
import cn.hnh.mapper.TagsMapper;
import cn.hnh.service.TagsService;
import cn.hnh.utils.DateFormatUtils;
import cn.hnh.vo.ArticleTitleVo;
import cn.hnh.vo.Result;
import cn.hnh.vo.TagsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author HuangNianHong
 * @Time 2021/1/5
 */
@Service
public class TagsServiceImpl implements TagsService {
    @Autowired
    private TagsMapper tagsMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public Result<TagsVo> getTagsVo() {
        TagsVo vo = new TagsVo();
        vo.setCount(tagsMapper.getTagsCount());
        vo.setInfo(tagsMapper.getTagsArticleVo());
        return new Result<>(ResultInfo.SUCCESS_CODE,ResultInfo.SUCCESS_MESSAGE,vo);
    }

    @Override
    public Result<List<ArticleTitleVo>> getArticleTitleVoByTagsName(String tagsName) {
        Integer id = tagsMapper.getIdByName(tagsName);
        if(id==null)
            return new Result<>(ResultInfo.NOTFOUND_CODE,ResultInfo.NOTFOUND_MESSAGE);
        List<ArticleTitleVo> list = articleMapper.getArticleTitleByTagsId(id);
        for (ArticleTitleVo articleTitleVo : list) {
            articleTitleVo.setDate(DateFormatUtils.formatDate(articleTitleVo.getTime()));
        }
        return new Result<>(ResultInfo.SUCCESS_CODE,ResultInfo.SUCCESS_MESSAGE,list);
    }
}
